﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;


namespace SLARToolKitWinPhoneSample
{
    public partial class M_bmi_bmr : PhoneApplicationPage
    {
        public M_bmi_bmr()
        {
            InitializeComponent();
        }
       private void HandleCheck(object sender, RoutedEventArgs e)
        {
           RadioButton rb = sender as RadioButton;
           //choiceTextBlock.Text = "You chose: " + rb.GroupName + ": " + rb.Name;
        }

        private void calculate_button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
               
                Double activity_value=0;
                if (rb1.IsChecked == true)
                {
                    activity_value = 1.2;
                }
                else if (rb2.IsChecked == true)
                {
                    activity_value = 1.375;
                }
                else if (rb3.IsChecked == true)
                {
                    activity_value = 1.55;
                }
                else if (rb4.IsChecked == true)
                {
                    activity_value = 1.725;
                }
                else if (rb5.IsChecked == true)
                {
                    activity_value = 1.9;
                }

                
                Double h = Convert.ToDouble(height_box.Text) / 100.0;
                Double w = Convert.ToDouble(weight_box.Text);
                Double age = Convert.ToDouble(age_box.Text);
                Double h2 = Convert.ToDouble(height_box.Text);

                if ((h < 1) || (h > 2.80))
                {
                    MessageBox.Show("Please key in valid height number.");
                    return;
                }
                else if ((h2 < 100) || (h2 > 280))
                {
                    MessageBox.Show("Please key in valid height number.");
                    return;
                }
                if ((age < 13) || (age > 120))
                {
                    MessageBox.Show("Please key in valid age number.");
                    return;
                }
                if ((w < 20) || (w > 400))
                {
                    MessageBox.Show("Please key in valid weight number.");
                    return;
                }
                Double bmi = w / (h * h);
                String bmi_value = Math.Round(bmi, 2).ToString();
                String bmi_text = returnDesc(bmi);
             
               
                Double bmr = (10 * w) + (6.25 * h2) - (5 * age) + 5;
                String bmr_value = Math.Round(bmr , 2).ToString();

                Double total_bmr = bmr * activity_value;
                String total_bmr_value = Math.Round(total_bmr , 2).ToString();

                 String final_bmi = "Your BMI is " + bmi_value + ".\n" + bmi_text + "\n\n Your BMR is " + bmr_value + ".\n\n Your total calories burn is " + total_bmr_value+".";



                 this.NavigationService.Navigate(new Uri("/M_bmi_bmr_result.xaml?msg=" + final_bmi , UriKind.Relative));
            }
            catch (Exception exc)
            {
                MessageBox.Show( "Please key in your details.");
                

            }


        }

        public string returnDesc(Double bmi)
        {
            if (bmi < 18.5)
                return "You're underweight.";
            else if (bmi <= 24.9)
                return "You're fit.";
            else if (bmi <= 29.9)
                return "You're overweight!!";
            else 
                return "You're obese!!!";
        }
        private void height_box_GotFocus(object sender, RoutedEventArgs e)
        {
            if (height_box.Text == "Height in cm")
                height_box.Text = "";
        }

        private void weight_box_GotFocus(object sender, RoutedEventArgs e)
        {
            if (weight_box.Text == "Weight in kg")
                weight_box.Text = "";
        }
        private void age_box_GotFocus(object sender, RoutedEventArgs e)
        {
            if (age_box.Text == "Age in years")
                age_box.Text = "";
        }
        private void height_box_LostFocus(object sender, RoutedEventArgs e)
        {
            if (height_box.Text == "")
                height_box.Text = "Height in cm";
        }

        private void weight_box_LostFocus(object sender, RoutedEventArgs e)
        {
            if (weight_box.Text == "")
                weight_box.Text = "Weight in kg";
        }
        private void age_box_LostFocus(object sender, RoutedEventArgs e)
        {
            if (age_box.Text == "")
                age_box.Text = "Age in years";
        }

    }
}